<template>
  <a-spin :spinning="confirmLoading">
    <j-form-container :disabled="formDisabled">
      <a-form-model ref="form" :model="model" :rules="validatorRules" slot="detail">
        <a-row>
          <a-col :span="24">
            <a-form-model-item label="标题" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="title">
              <a-input v-model="model.title" placeholder="请输入标题" @blur="formatName"></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item :label="model.numberType" :labelCol="labelCol" :wrapperCol="wrapperCol"
                               prop="followIdArr">
              <!--              <a-input v-model="model.followId" placeholder="请输入跟进ID"  ></a-input>-->
              <!--              <a-input  placeholder="请输入跟进ID"  disabled></a-input>-->
              <a-select
                mode="multiple"
                v-model="model.followIdArr"
                style="width: 100%"
                placeholder="Please select"
                disabled
              >
                <!--                model.followIdArr-->
              </a-select>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="执行人" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="executeType">
              <j-checkbox
                v-model="model.executeType"
                :options="jCheckboxOptions"
                @change="zhixingrenChange"
              />
            </a-form-model-item>
          </a-col>
          <a-col :span="24" v-if="customCustomerIdShow">
            <a-form-model-item label="自定义" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="customIds">
              <!--              <a-input v-model="model.customerId" placeholder="请输入客户ID"  ></a-input>-->
              <!--              <JSelectMultiUser v-model="model.customerId" :query-config="selectUserQueryConfig" :url="kehuurl" placeholder="请选择客户"/>-->
              <JSelectUserByDepCustom placeholder="请选择" v-model="model.customIds"
              ></JSelectUserByDepCustom>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="跟进时间" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="followTime">
              <!--              <j-date placeholder="请选择跟进时间" v-model="model.followTime"  style="width: 100%" show-time format="yyyy-MM-dd HH:mm:ss"/>-->
              <a-date-picker
                style="width: 100%"
                placeholder="请选择跟进时间"
                :show-time="true"
                v-model="model.followTime"
                format="YYYY-MM-DD HH:mm:ss"
                :disabled-date="disabledDate"
                @change="priceExamineTimeChange"
              />
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="提醒" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="remind">
              <j-dict-select-tag type="radioButton" v-model="model.remind" dictCode="follow_remind_time"
                                 placeholder="请选择提醒时间" @change="customFloowTime2Change" />
            </a-form-model-item>
          </a-col>
          <a-col :span="24" v-if="customfollowTime2Show">
            <a-form-model-item label="提醒时间" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="customTime">
              <a-date-picker
                style="width: 100%"
                placeholder="请选择提醒时间"
                :show-time="true"
                v-model="model.customTime"
                format="YYYY-MM-DD HH:mm:ss"
                :disabled-date="disabledDate"
                @change="priceExamineTimeChange2"
              />
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="提醒内容" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="remark">
              <!--              <a-input v-model="model.remark" placeholder="请输入提醒备注"  ></a-input>-->
              <a-textarea :rows="3" v-model="model.remark" placeholder="请输入提醒备注" @blur="remarkFormat" />
            </a-form-model-item>
          </a-col>

          <a-col :span="24">
            <a-form-model-item label="跟进方式" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="mode">
              <!--              <j-search-select-tag v-model="model.mode" dict="follow_mode"  type="radioButton" />-->
              <j-dict-select-tag type="radioButton" v-model="model.mode" dictCode="follow_mode" />
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="跟进业务类型" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="type">
              <!--              <j-search-select-tag v-model="model.type" dict="follow_type"  type="radioButton" />-->
              <j-dict-select-tag type="radioButton" v-model="model.type" dictCode="followremind_type"
                                 :disabled="followShow" />
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="加入访客" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="addVisitor">
              <a-switch @change="switchOnChange" v-model="addVisitor" />
            </a-form-model-item>
          </a-col>
          <!--          <a-col :span="24">
                      <a-form-model-item label="业务类型" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="businessId">
                        <a-input v-model="model.businessId" placeholder="请输入业务类型"  ></a-input>
                        <j-search-select-tag v-model="model.businessId" dict="follow_type"  />
                      </a-form-model-item>
                    </a-col>-->
          <!--          <a-col :span="24">
                      <a-form-model-item label="创建时间" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="createTime">
                        <j-date placeholder="请选择创建时间" v-model="model.createTime"  style="width: 100%" />
                      </a-form-model-item>
                    </a-col>
                    <a-col :span="24">
                      <a-form-model-item label="是否删除(0.未删除 1.已删除)" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="isDelete">
                        <a-input-number v-model="model.isDelete" placeholder="请输入是否删除(0.未删除 1.已删除)" style="width: 100%" />
                      </a-form-model-item>
                    </a-col>-->
        </a-row>
      </a-form-model>
    </j-form-container>
  </a-spin>
</template>

<script>

import { httpAction, getAction } from '@/api/manage'
import { validateDuplicateValue } from '@/utils/util'
import JSelectMultiUser from '@/components/jeecgbiz/JSelectMultiUserCustom'
import JCheckbox from '@/components/jeecg/JCheckbox'
import JSelectUserByDepCustom from '@/components/jeecgbiz/JSelectUserByDepCustom'
import moment from 'moment'

export default {
  name: 'FollowRemindForm',
  components: {
    JSelectMultiUser,
    JCheckbox,
    JSelectUserByDepCustom
  },
  props: {
    //表单禁用
    disabled: {
      type: Boolean,
      default: false,
      required: false
    }
  },
  watch: {
    addVisitor: {
      deep: true,
      handler(newValue, oldValue) {
        if (newValue) {
          this.model.addVisitor = 1
        } else {
          this.model.addVisitor = 0
        }
      }
    }
  },
  data() {
    return {
      followShow: false,
      addVisitor: false,
      jCheckboxOptions: [
        { label: '负责人', value: '1' },
        { label: '协同人', value: '2' },
        { label: '自定义', value: '3' }
      ],
      customCustomerIdShow: false,
      customfollowTime2Show: false,
      model: {
        addVisitor: 0
      },
      labelCol: {
        xs: { span: 24 },
        sm: { span: 5 }
      },
      wrapperCol: {
        xs: { span: 24 },
        sm: { span: 16 }
      },
      confirmLoading: false,
      validatorRules: {
        title: [
          { required: true, message: '请输入标题!' }
        ],
        followId: [
          { required: true, message: '请输入跟进!' }
        ],
        executeType: [
          { required: true, message: '执行人不能为空!' }
        ],
        followTime: [
          { required: true, message: '下次跟进时间不能为空!' },
          {
            validator: (rule, value, callback) => {
              console.log('value', value)
              let NendTime = value.replace(new RegExp('-', 'g'), '/')
              if (new Date().getTime() < new Date(NendTime).getTime()) {
                callback()
              } else {
                callback(false)
              }

            },
            message: '跟进时间必须大于当前时间'
          }
        ],
        remind: [
          { required: true, message: '提醒时间不能为空!' },
          {
            validator: (rule, value, callback) => {
              //表单上面填的提醒时间
              let tixingTime = null
              //系统当前时间
              let nowTime = new Date().getTime()

              if (this.model.followTime) {
                tixingTime = this.model.followTime.replace(new RegExp('-', 'g'), '/')
              } else {
                callback(new Error('请先选择跟进时间'))
              }

              switch (value) {
                case '1':
                  let m15 = 15 * 60 * 1000
                  if ((new Date(tixingTime).getTime() - m15) > nowTime) {
                    callback()
                  } else {
                    callback(new Error('提醒时间不能早于跟进时间'))
                  }
                  break
                case '2':
                  let h1 = 60 * 60 * 1000
                  if ((new Date(tixingTime).getTime() - h1) > nowTime) {
                    callback()
                  } else {
                    callback(new Error('提醒时间不能早于跟进时间'))
                  }
                  break
                case '3':
                  let d1 = 24 * 60 * 60 * 1000
                  if ((new Date(tixingTime).getTime() - d1) > nowTime) {
                    callback()
                  } else {
                    callback(new Error('提醒时间不能早于跟进时间'))
                  }
                  break
                case '4':
                  let w1 = 7 * 24 * 60 * 60 * 1000
                  if ((new Date(tixingTime).getTime() - w1) > nowTime) {
                    callback()
                  } else {
                    callback(new Error('提醒时间不能早于跟进时间'))
                  }
                  break
                default:
                  callback()
                  break
              }
            },
            trigger: ['change']
          }
        ],
        customIds: [
          { required: true, message: '不能为空!' }
        ],
        businessId: [
          { required: true, message: '请输入业务类型!' }
        ],
        type: [
          { required: true, message: '不能为空!' }
        ],
        mode: [
          { required: true, message: '不能为空!' }
        ],
        remark: [
          { required: true, message: '不能为空!' }
        ],
        customTime: [
          { required: true, message: '不能为空!' },
          {
            validator: (rule, value, callback) => {
              console.log('value', value)
              let NendTime = value.replace(new RegExp('-', 'g'), '/')
              if (new Date().getTime() < new Date(NendTime).getTime()) {
                callback()
              } else {
                callback(false)
              }

            },
            message: '提醒时间必须大于当前时间'
          }
        ]
      },
      url: {
        // add: "/followremind/followRemind/add",
        add: '/followremind/followRemind/add',
        edit: '/followremind/followRemind/edit',
        queryById: '/followremind/followRemind/queryById'
      }
    }
  },
  computed: {
    formDisabled() {
      return this.disabled
    }
  },
  created() {
    //备份model原始值
    this.modelDefault = JSON.parse(JSON.stringify(this.model))
  },
  methods: {
    //提醒标题格式化
    formatName() {
      let title = this.model.title
      this.model.title = title.trim()
    },
    //提醒内容格式化
    remarkFormat() {
      let remark = this.model.remark
      this.model.remark = remark.trim()
    },
    switchOnChange(e) {
      console.log(e)
    },
    priceExamineTimeChange(e, e1) {
      this.model.followTime = e1
    },
    priceExamineTimeChange2(e, e1) {
      this.model.customTime = e1
    },
    disabledDate(current) {
      return current && current < moment().subtract(1, 'days').endOf('day')
    },
    customFloowTime2Change(e) {
      if (e == 5) {
        this.customfollowTime2Show = true
      } else {
        this.customfollowTime2Show = false

      }
    },
    add(record) {
      this.edit(record)
      console.log('add===record', record)
      // this.edit(this.modelDefault);
    },
    edit(record) {
      this.model = Object.assign({}, record)
      console.log('编辑状态record', record)
      // let txt1 = record.followIdArr.split(',')[0]
      //下面这行从父组件去定义标题
      // this.model.title = record.followIdArr[0] + '等' + record.followIdArr.length + '个报价单的跟进提醒'
      // this.model.customerId = record.customer_id
      console.log('model', this.model.followIdArr)
      this.visible = true
    },
    submitForm() {
      console.log('=======', this.model)
      const that = this
      // 触发表单验证
      this.$refs.form.validate(valid => {
        if (valid) {
          that.confirmLoading = true
          let httpurl = ''
          let method = ''
          if (!this.model.id) {
            httpurl += this.url.add
            method = 'post'
          } else {
            httpurl += this.url.edit
            method = 'put'
          }
          httpAction(httpurl, this.model, method).then((res) => {
            if (res.success) {
              that.$message.success(res.message)
              that.$emit('ok')
            } else {
              that.$message.warning(res.message)
            }
          }).finally(() => {
            that.confirmLoading = false
          })
        }

      })
    },
    //执行人
    zhixingrenChange(e) {
      // customCustomerIdShow
      console.log('e', e)
      let arr = e.split(',')
      if (arr.includes('3')) {
        this.customCustomerIdShow = true
      } else {
        this.customCustomerIdShow = false

      }
    }
  }
}
</script>